//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "FirstDerivativeOp.h"
using namespace Cephei::QL::Experimental::Finitedifferences;
#include <gen/QL/Experimental/Finitedifferences/FdmMesher.h>
#include <gen/QL/Math/Array.h>
#include <gen/QL/Experimental/Finitedifferences/TripleBandLinearOp.h>
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Finitedifferences::CFirstDerivativeOp::CFirstDerivativeOp (UInt64 direction, Cephei::QL::Experimental::Finitedifferences::IFdmMesher^ mesher) : CTripleBandLinearOp(CFirstDerivativeOp::typeid)
{
    CFdmMesher^ _Cmesher;
    try
    {
#ifdef HANDLE
        _phFirstDerivativeOp = NULL;
#endif
        QuantLib::Size _direction = (QuantLib::Size)ValueHelper::Convert (direction);
        _Cmesher = safe_cast<CFdmMesher^> (mesher);
        _Cmesher->Lock();
        boost::shared_ptr<QuantLib::FdmMesher>& _mesher = static_cast<boost::shared_ptr<QuantLib::FdmMesher>&> (_Cmesher->GetShared ()); 
        _ppFirstDerivativeOp = new boost::shared_ptr<QuantLib::FirstDerivativeOp> (new QuantLib::FirstDerivativeOp ( _direction,  _mesher ));
        SetTripleBandLinearOp (boost::dynamic_pointer_cast<QuantLib::TripleBandLinearOp> (*_ppFirstDerivativeOp));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cmesher != nullptr) _Cmesher->Unlock();
    }
}
Cephei::QL::Experimental::Finitedifferences::CFirstDerivativeOp::CFirstDerivativeOp (boost::shared_ptr<QuantLib::FirstDerivativeOp>& childNative, Object^ owner) : CTripleBandLinearOp(CFirstDerivativeOp::typeid)
{
#ifdef HANDLE
	_phFirstDerivativeOp = NULL;
#endif
	_ppFirstDerivativeOp = &childNative;
    _ppTripleBandLinearOp = new boost::shared_ptr<QuantLib::TripleBandLinearOp> (boost::dynamic_pointer_cast<QuantLib::TripleBandLinearOp> (*_ppFirstDerivativeOp));
}
Cephei::QL::Experimental::Finitedifferences::CFirstDerivativeOp::CFirstDerivativeOp (QuantLib::FirstDerivativeOp& childNative, Object^ owner) : CTripleBandLinearOp(CFirstDerivativeOp::typeid)
{
#ifdef HANDLE
	_phFirstDerivativeOp = NULL;
#endif
	_ppFirstDerivativeOp = new boost::shared_ptr<QuantLib::FirstDerivativeOp> (&childNative);
    _ppTripleBandLinearOp = new boost::shared_ptr<QuantLib::TripleBandLinearOp> (boost::dynamic_pointer_cast<QuantLib::TripleBandLinearOp> (*_ppFirstDerivativeOp));
    _FirstDerivativeOpOwner = owner;
    _TripleBandLinearOpOwner = owner;
}

Cephei::QL::Experimental::Finitedifferences::CFirstDerivativeOp::CFirstDerivativeOp (CFirstDerivativeOp^ copy) : CTripleBandLinearOp(CFirstDerivativeOp::typeid)
{
#ifdef HANDLE
	_phFirstDerivativeOp = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFirstDerivativeOp = new boost::shared_ptr<QuantLib::FirstDerivativeOp> (copy->GetShared());
        _ppTripleBandLinearOp = new boost::shared_ptr<QuantLib::TripleBandLinearOp> (boost::dynamic_pointer_cast<QuantLib::TripleBandLinearOp> (*_ppFirstDerivativeOp));
    }
}
Cephei::QL::Experimental::Finitedifferences::CFirstDerivativeOp::CFirstDerivativeOp (System::Type^ t) : CTripleBandLinearOp(CFirstDerivativeOp::typeid)
{
#ifdef HANDLE
	_phFirstDerivativeOp = NULL;
#endif
	if (!t->IsSubclassOf(CFirstDerivativeOp::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Finitedifferences::CFirstDerivativeOp::CFirstDerivativeOp (QuantLib::Handle<QuantLib::FirstDerivativeOp>& childNative, Object^ owner)  : CTripleBandLinearOp(CFirstDerivativeOp::typeid)
{
	_phFirstDerivativeOp = &childNative;
	_ppFirstDerivativeOp = &static_cast<boost::shared_ptr<QuantLib::FirstDerivativeOp>>(childNative.currentLink());
    _ppTripleBandLinearOp = new boost::shared_ptr<QuantLib::TripleBandLinearOp> (boost::dynamic_pointer_cast<QuantLib::TripleBandLinearOp> (*_ppFirstDerivativeOp));
    _FirstDerivativeOpOwner = owner;
}
Cephei::QL::Experimental::Finitedifferences::CFirstDerivativeOp::CFirstDerivativeOp (QuantLib::Handle<QuantLib::FirstDerivativeOp> childNative)  : CTripleBandLinearOp(CFirstDerivativeOp::typeid)
{
	_phFirstDerivativeOp = &childNative;
	_ppFirstDerivativeOp = &static_cast<boost::shared_ptr<QuantLib::FirstDerivativeOp>>(childNative.currentLink());
    _ppTripleBandLinearOp = new boost::shared_ptr<QuantLib::TripleBandLinearOp> (boost::dynamic_pointer_cast<QuantLib::TripleBandLinearOp> (*_ppFirstDerivativeOp));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Finitedifferences::CFirstDerivativeOp::CFirstDerivativeOp (QuantLib::FirstDerivativeOp childNative)  : CTripleBandLinearOp(CFirstDerivativeOp::typeid)
{
#ifdef HANDLE
	_phFirstDerivativeOp = NULL;
#endif
	_ppFirstDerivativeOp = new boost::shared_ptr<QuantLib::FirstDerivativeOp> (new QuantLib::FirstDerivativeOp (childNative));
    _ppTripleBandLinearOp = new boost::shared_ptr<QuantLib::TripleBandLinearOp> (boost::dynamic_pointer_cast<QuantLib::TripleBandLinearOp> (*_ppFirstDerivativeOp));
}
#endif

Cephei::QL::Experimental::Finitedifferences::CFirstDerivativeOp::~CFirstDerivativeOp ()
{
    if (_ppFirstDerivativeOp != NULL)
    {
	    delete _ppFirstDerivativeOp;
        _ppFirstDerivativeOp = NULL;
    }
}
Cephei::QL::Experimental::Finitedifferences::CFirstDerivativeOp::!CFirstDerivativeOp ()
{
    if (_ppFirstDerivativeOp != NULL)
    {
	    delete _ppFirstDerivativeOp;
    }
}
QuantLib::FirstDerivativeOp& Cephei::QL::Experimental::Finitedifferences::CFirstDerivativeOp::GetReference ()
{
    if (_ppFirstDerivativeOp == NULL) throw gcnew NativeNullException ();
	return **_ppFirstDerivativeOp;
}
boost::shared_ptr<QuantLib::FirstDerivativeOp>& Cephei::QL::Experimental::Finitedifferences::CFirstDerivativeOp::GetShared ()
{
    if (_ppFirstDerivativeOp == NULL) throw gcnew NativeNullException ();
	return *_ppFirstDerivativeOp;
}
QuantLib::FirstDerivativeOp* Cephei::QL::Experimental::Finitedifferences::CFirstDerivativeOp::GetPointer ()
{
    if (_ppFirstDerivativeOp == NULL) throw gcnew NativeNullException ();
	return &**_ppFirstDerivativeOp;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FirstDerivativeOp>& Cephei::QL::Experimental::Finitedifferences::CFirstDerivativeOp::GetHandle ()
{
	if (_phFirstDerivativeOp == NULL)
	{
		_phFirstDerivativeOp = new Handle<QuantLib::FirstDerivativeOp> (*_ppFirstDerivativeOp);
	}
	return *_phFirstDerivativeOp;
}
#endif
bool Cephei::QL::Experimental::Finitedifferences::CFirstDerivativeOp::HasNative () 
{
	return (_ppFirstDerivativeOp != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Finitedifferences::IFirstDerivativeOp^ Cephei::QL::Experimental::Finitedifferences::CFirstDerivativeOp_Factory::Create (UInt64 direction, Cephei::QL::Experimental::Finitedifferences::IFdmMesher^ mesher)
{
    return gcnew CFirstDerivativeOp ( direction,  mesher);
}
